package com.liugl.helloworld

import java.util.*
import kotlin.math.max

//求和
fun add(a: Int, b: Int): Int = a + b

//使用内置函数完成大小判断
fun maxNum(a: Int, b: Int) = max(a, b)

//使用条件判断语句
fun maxNum2(a: Int, b: Int): Int = if (a > b) a else b

//获取成绩
fun getScore(name: String): Int = when (name) {
    "Tom" -> 86
    "Jim" -> 77
    "Jack" -> 95
    "Lily" -> 100
    else -> 0
}

fun checkNum(num: Number) = when (num) {
    is Int -> println("number $num is Int")
    is Double -> println("number $num is Double")
    else -> println("number $num is not support")
}

fun getScore2(name: String) = when {
    name.startsWith("Tom") -> 86
    name == "Jim" -> 77
    name == "Jack" -> 95
    name == "Lily" -> 100
    else -> 0
}

fun printLoopNum(max: Int) {
    for (i in 0..max) println("num $i")
    println("----------")
    for (i in 0 until max) println("num $i")
    println("----------")
    for (i in 0 until max step 2) println("num $i")
    println("----------")
    for (i in max downTo 0) println("num $i")
}

fun getList(): List<String> {
    val list = listOf<String>("Apple", "Pear", "Orange", "Banana", "Grape")

    for (fruit in list) {
        println(fruit)
    }

    return list
}

//获取长度可变的列表
fun getChangeList(): List<String> {
    val list = mutableListOf<String>("Apple", "Pear", "Orange", "Banana", "Grape")
    list.add("Watermelon")

//    for (fruit in list) {
//        println(fruit)
//    }

    return list
}

//传统方式获取最长字符串
fun getMaxLen(list: List<String>) {
    var maxStr = ""
    for (f in list) {
        if (f.length > maxStr.length) maxStr = f
    }

    println("The max length word is $maxStr")
}

//kotlin简化方式获取最长字串
fun kotlinGetMaxStr(list: List<String>) {
    val maxLenWord = list.maxByOrNull { it.length }

    println("The max length word is $maxLenWord")
}

//测试键值对
fun mapTest(): Map<String, Int> {
    val map = hashMapOf("Apple" to 1, "Pear" to 2)
    map["Banana"] = 3
    map["Orange"] = 4
    map["Grape"] = 5

//    for ((fruit, index) in map) {
//        println("key $fruit --- value $index")
//    }

    return map
}

//lambda函数
fun lambdaTest() {
    val lam = { fruit: String -> fruit.length }
    val maxLenFruit = getChangeList().maxByOrNull(lam)
    println("max is $maxLenFruit")
}

//将列表中的每个元素都转换为大写
fun toUpperCase() {
    val list = getChangeList().map { it.toUpperCase(Locale.ROOT) }
    for (fruit in list) {
        println("fruit up to $fruit")
    }
}

//过滤
fun filterList() {
    val list = getChangeList().filter { it.length <= 5 }.map { it.toUpperCase(Locale.ROOT) }
    for (fruit in list) {
        println(fruit)
    }
}

//any和all函数
fun anyAndAll() {
    val list = getChangeList()
    val anyList = list.any { it.length <= 5 }
    val allList = list.all { it.length <= 5 }

    println("any result:$anyList -- all result:$allList")
}

//java函数式api
fun javaFun() {
    Thread {
        println("Thread is runing")
    }.start()
}

//进行学习
fun doStudy(student: Student?) {
    student?.doHomework()
    student?.readBooks()
}

//进行学习使用let优化
fun fixDoStudy(student: Student?) {
    student?.let {
        it.doHomework()
        it.readBooks()
    }
}

//获取字符串长度
fun getStrLen(str: String?): Int = str?.length ?: 0

fun main() {
}

